package com.example.yingjiguanli_demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.yingjiguanli_demo.pojo.MentorProfiles;
import com.example.yingjiguanli_demo.mapper.MentorProfilesMapper;
import com.example.yingjiguanli_demo.pojo.Result;
import com.example.yingjiguanli_demo.service.IMentorProfilesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.yingjiguanli_demo.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

import static com.example.yingjiguanli_demo.utils.ConstantUtils.FILE_SERVER;
import static com.example.yingjiguanli_demo.utils.ConstantUtils.UPLOAD_DIR;

/**
 * <p>
 * 导师详细信息表 服务实现类
 * </p>
 *
 * @author scj
 * @since 2024-12-07
 */
@Service
public class MentorProfilesServiceImpl extends ServiceImpl<MentorProfilesMapper, MentorProfiles> implements IMentorProfilesService {

    @Autowired
    private MentorProfilesMapper mentorProfilesMapper;
    @Autowired
    private UserUtils userUtils;

    /**
     * 上传导师信息
     * @param sessionId sessionId
     * @param bio 导师简介
     * @param researchFields 研究方向
     * @param contactInfo 联系方式
     * @param profilePicture 头像
     */
    @Override
    @Transactional
    public void uploadMentorProfile(String sessionId
            , String bio
            , String mentorName
            , String researchFields
            , String contactInfo
            , MultipartFile profilePicture) {
        // 保存头像文件
        String fileName = profilePicture.getOriginalFilename();
        // 生成新的文件名
        int userId = userUtils.getUserId(sessionId);
        String userName = userUtils.getUserName(sessionId);
        String newFileName = userName + "_" + fileName;
        File uploadDir = new File(UPLOAD_DIR);
        if (!uploadDir.exists()) {
            uploadDir.mkdirs(); // 如果上传目录不存在，则创建目录
        }

        // 保存文件到本地
        try {
            Path filePath = Paths.get(UPLOAD_DIR, newFileName);
            profilePicture.transferTo(filePath); // 保存文件到指定目录



            // 创建 MentorProfiles 对象，保存导师信息
            MentorProfiles mentorProfiles = new MentorProfiles();
            mentorProfiles.setMentorId(userId);
            mentorProfiles.setBio(bio);
            mentorProfiles.setResearchFields(researchFields);
            mentorProfiles.setContactInfo(contactInfo);
            mentorProfiles.setMentorName(mentorName);
            mentorProfiles.setProfilePicture(newFileName); // 保存文件路径

            // 保存导师信息到数据库
            mentorProfilesMapper.insert(mentorProfiles);

        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("文件上传失败");
        }
    }

    /**
     * 获取导师信息
     * @return 导师信息列表
     */
    @Override
    public Result<List<MentorProfiles>> getMentorProfile() {
        QueryWrapper<MentorProfiles> queryWrapper = new QueryWrapper<>();
        List<MentorProfiles> mentorProfiles = mentorProfilesMapper.selectList(queryWrapper);
        return Result.success(mentorProfiles);
    }

    /**
     *  获取导师信息
     * @param id id
     * @return 导师信息
     */
    @Override
    public MentorProfiles getMentorProfileById(String id) {
        QueryWrapper<MentorProfiles> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mentor_id", id);
        return mentorProfilesMapper.selectOne(queryWrapper);
    }

    /**
     * 获取导师头像
     * @param fileName 文件名
     * @return 图片文件字节数组
     */
    @Override
    public byte[] getProfilePicture(String fileName) {
        try {
            Path filePath = Paths.get(UPLOAD_DIR, fileName);
            return Files.readAllBytes(filePath); // 读取图片文件并返回
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("获取图片失败");
        }
    }
}
